package GUI;

import Controler.readfile;
import Model.MonteCarloSimulation;
import MultiThread.Multi3;
import MultiThread.Multi4;
import SimpleThread.Multi;
import SimpleThread.Multi2;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.StringTokenizer;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;

public class Gui extends JFrame implements ActionListener {

	private int line;
	private int line2;
	private static String linetxt;
	private JComboBox linechoiceJCB = new JComboBox();
	private JComboBox threadchoiceJCB = new JComboBox();
	private JButton button1 = new JButton("Simulation");
	private JProgressBar jpb = new JProgressBar(0);
	private JLabel r = new JLabel();
	private JLabel duree=new JLabel();
	private JLabel exetime=new JLabel();

	public Gui(){
		this.setTitle("Kopczuk - Daireaux - MonteCarlo");
		this.setSize(400, 450); 
		JPanel panel = new JPanel();     
		this.setContentPane(panel);
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
		jpb.setValue(0);
		jpb.setStringPainted(true);
		linechoiceJCB.addItem("Line 1");
		linechoiceJCB.addItem("Line 2");
		linechoiceJCB.addItem("Line 3");
		linechoiceJCB.addItem("Line 4");
		linechoiceJCB.addItem("Line 5");
		linechoiceJCB.addItem("Line 6");
		linechoiceJCB.addItem("Line 7");
		linechoiceJCB.addItem("Line 8");
		linechoiceJCB.addItem("Line 9");
		linechoiceJCB.addItem("Line 10");
		threadchoiceJCB.addItem("Simple Thread");
		threadchoiceJCB.addItem("Multi Thread");
		JPanel panel0=new JPanel();
		JPanel panel1=new JPanel();
		JPanel panel2=new JPanel();
		JPanel panel3=new JPanel();
		JPanel panel4=new JPanel();
		JPanel res=new JPanel();
		JPanel t=new JPanel();
		panel.add(panel0);
		panel.add(panel1);
		panel.add(panel2);
		panel.add(panel3);
		panel.add(t);
		panel.add(res);
		panel.add(panel4);
		JLabel linechoice = new JLabel("Choose your Line");
		JLabel threadchoice = new JLabel("Choose your Thread Type");
		panel1.add(linechoice);
		panel2.add(threadchoice);
		panel1.add(linechoiceJCB);
		panel2.add(threadchoiceJCB);
		panel3.add(button1);
		panel3.add(jpb);
		t.add(duree);
		res.add(r);
		panel4.add(exetime);
		linechoiceJCB.addActionListener(this);
		threadchoiceJCB.addActionListener(this);
		button1.addActionListener(this);
		this.setVisible(true);
		this.setLocationRelativeTo(null);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub

		MonteCarloSimulation simulation=new MonteCarloSimulation();
		readfile ReadFile1 = new readfile();

		if(arg0.getSource()==linechoiceJCB){
			line=linechoiceJCB.getSelectedIndex();
			line++;
			try {
				linetxt = ReadFile1.read(line);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		if(arg0.getSource()==threadchoiceJCB){
			line2=threadchoiceJCB.getSelectedIndex();
			line2++;
		}

		if((arg0.getSource() == button1)&&(line2==1)&&(line>=1)&&(line<=10)){
			double result;
			StringTokenizer splitline = new StringTokenizer(linetxt,","); 
			String[] items = new String[splitline.countTokens()] ; 
			int i = 0;
			while ( splitline.hasMoreTokens() )
			{ 
				items[i] = splitline.nextToken(); 
				i++;
			}
			String callputflag=items[0];
			String A=items[1];
			String Z=items[2];
			String E=items[3];
			String R=items[4];
			String T=items[5];
			String Y=items[6];
			String U=items[7];
			String I=items[8];

			double price = Double.parseDouble(A);
			double strike_price = Double.parseDouble(Z);
			double time_year = Double.parseDouble(E);
			double rate = Double.parseDouble(R);
			double rate_holding = Double.parseDouble(T);
			double volatility = Double.parseDouble(Y);
			int nsteps = Integer.parseInt(U);
			int nsimulations = Integer.parseInt(I);
			simulation.set_parameters(callputflag, price, strike_price, time_year,rate, rate_holding, volatility, nsteps, nsimulations);
			Multi t1 =  new Multi("A",this,simulation,linetxt);
			Multi2 t2=new Multi2("B",this,simulation);
			t1.start();
			t2.start();
		}

		else if((arg0.getSource() == button1)&&(line2==2)&&(line>=1)&&(line<=10)){
			double result;
			StringTokenizer splitline = new StringTokenizer(linetxt,","); 
			String[] items = new String[splitline.countTokens()] ; 
			int i = 0;
			while ( splitline.hasMoreTokens() )
			{ 
				items[i] = splitline.nextToken(); 
				i++;
			}
			String callputflag=items[0];
			String A=items[1];
			String Z=items[2];
			String E=items[3];
			String R=items[4];
			String T=items[5];
			String Y=items[6];
			String U=items[7];
			String I=items[8];

			double price = Double.parseDouble(A);
			double strike_price = Double.parseDouble(Z);
			double time_year = Double.parseDouble(E);
			double rate = Double.parseDouble(R);
			double rate_holding = Double.parseDouble(T);
			double volatility = Double.parseDouble(Y);
			int nsteps = Integer.parseInt(U);
			int nsimulations = Integer.parseInt(I);
			simulation.set_parameters(callputflag, price, strike_price, time_year,rate, rate_holding, volatility, nsteps, nsimulations);
			Multi3 t3 =  new Multi3("A",this,simulation,linetxt);
			Multi4 t4 = new Multi4("B",this,simulation);
			t3.start();
			t4.start();
		}

	}

	public JComboBox getLinechoiceJCB() {
		return linechoiceJCB;
	}

	public void setLinechoiceJCB(JComboBox linechoiceJCB) {
		this.linechoiceJCB = linechoiceJCB;
	}

	public JComboBox getThreadchoiceJCB() {
		return threadchoiceJCB;
	}

	public void setThreadchoiceJCB(JComboBox threadchoiceJCB) {
		this.threadchoiceJCB = threadchoiceJCB;
	}

	public JButton getButton1() {
		return button1;
	}

	public void setButton1(JButton button1) {
		this.button1 = button1;
	}

	public JProgressBar getJpb() {
		return jpb;
	}

	public void setJpb(JProgressBar jpb) {
		this.jpb = jpb;
	}

	public JLabel getR() {
		return r;
	}

	public void setR(JLabel r) {
		this.r = r;
	}

	public JLabel getDuree() {
		return duree;
	}

	public void setDuree(JLabel duree) {
		this.duree = duree;
	}

	public int getLine() {
		return line;
	}

	public void setLine(int line) {
		this.line = line;
	}

	public int getLine2() {
		return line2;
	}

	public void setLine2(int line2) {
		this.line2 = line2;
	}

	public static String getLinetxt() {
		return linetxt;
	}

	public static void setLinetxt(String linetxt) {
		Gui.linetxt = linetxt;
	}

	public JLabel getExetime() {
		return exetime;
	}

	public void setExetime(JLabel exetime) {
		this.exetime = exetime;
	}
}




















